<template>
  <div>
    <!-- Form -->
    <el-form :inline="true" :model="courseQuery" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="courseQuery.title" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="courseQuery.status" placeholder="发布状态">
          <el-option label="已发布" value="Normal"></el-option>
          <el-option label="未发布" value="Draft"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getCourseList()">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- List -->
    <el-table :data="courseList" style="width: 100%">
      <el-table-column type="index" prop="no" label="序号" width="50%">
      </el-table-column>
      <el-table-column prop="title" label="课程名称" width="80">
      </el-table-column>
      <el-table-column prop="status" label="课程状态" width="100">
        <template slot-scope="scope">
          {{ scope.row.status == "Draft" ? "未发布" : "已发布" }}
        </template>
      </el-table-column>
      <el-table-column prop="lessonNum" label="课时数" width="100">
      </el-table-column>
      <el-table-column prop="gmtCreate" label="添加时间" width="180">
      </el-table-column>
      <el-table-column
        prop="viewCount"
        label="浏览数量"
        width="50px"
      ></el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="updateCourseInfo(scope.row.id)"
            >编辑课程基本信息</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="updateCourseChapter(scope.row.id)"
            >编辑课程大纲信息</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-delete"
            @click="ShowDeleMessage(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="getCourseList"
        @current-change="getCourseList"
        :current-page="page"
        :page-size="pageSize"
        layout="total,  prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import courseApi from "@/api/edu/course";
export default {
  data() {
    return {
      value: [],
      total: 0, //总数
      page: 1, //当前页
      pageSize: 10, //每页记录数
      courseList: [],
      // 用于查询
      courseQuery: {},
      // 删除框
      dialogVisible: false
    };
  },
  created() {
    this.getCourseList();
  },
  methods: {
    getCourseList(page = 1) {
      //当页面发生变化时 会自动返回所点击的页码作为参数
      console.log(page);
      this.page = page;
      courseApi
        .getCourseList(this.page, this.pageSize, this.courseQuery)
        .then(resonse => {
          this.courseList = resonse.data.courseList.records;
          this.total = resonse.data.courseList.total;

          console.log(resonse);
        })
        .catch(error => {});
    },
    ShowDeleMessage(courseId) {
      this.$confirm("此操作将永久删除该讲师记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          courseApi
            .deleCourse(courseId)
            .then(response => {
              //删除成功
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getCourseList();
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    updateCourseInfo(courseId) {
      this.$router.push({
        path: "/Learning/course/info/" + courseId
      });
    },
    updateCourseChapter(courseId) {
      this.$router.push({
        path: "/Learning/course/chapter/" + courseId
      });
    }
  }
};
</script>
